/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.jiopi.ibean.remote.rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.jiopi.framework.annotation.module.InstanceType;
import org.jiopi.framework.annotation.module.Instantiation;
import org.jiopi.framework.annotation.module.RegisterModule;
import org.jiopi.remote.RemoteCentralConsole;

/**
 * 
 * iBean remote central console RMI client implement
 * 
 * @version 0.5.0.1
 * @since 0.5.0.1
 * @author netstarry
 *
 */
@RegisterModule
@Instantiation(type=InstanceType.CONFIGURATION_SINGLETON,initMethod="init")
public class IBeanRemoteCentralConsoleRMI implements RemoteCentralConsole {
	
	private RemoteCentralConsoleRMI _rccRMI;
	
	/**
	 * 主机名/IP
	 * 
	 * 默认本机
	 */
	private String _host = "127.0.0.1";
	
	/**
	 * 端口号
	 * 
	 * 默认1099
	 */
	private int _port = 1099;
	
	/**
	 * 设置RMI主机名/IP
	 * @param host
	 */
	public void setHost(String host){
		this._host = host;
	}
	
	/**
	 * 设置RMI
	 * @param port
	 */
	public void setPort(int port){
		this._port = port;
	}
	
	public void init(){
		try {
			Registry remoteRegistry = LocateRegistry.getRegistry(_host,_port);
			_rccRMI = (RemoteCentralConsoleRMI) remoteRegistry.lookup("jiopi.remote");
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		} catch (NotBoundException e) {
			throw new RuntimeException(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T methodCallStrict(String containerID, String moduleName,
			String compatibleVersion, String controlPanelName,
			String methodName, Class<T> returnType, Class<?>[] parameterTypes,
			Object... args) {
		try {
			return (T) _rccRMI.methodCallStrict(containerID, moduleName, compatibleVersion, controlPanelName, methodName, parameterTypes, args);
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T methodCall(String containerID, String moduleName,
			String compatibleVersion, String controlPanelName,
			String methodName, Class<T> returnType, Object... args) {
		try {
			return (T) _rccRMI.methodCall(containerID, moduleName, compatibleVersion, controlPanelName, methodName, args);
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}

	

}
